package com.efast.cafe.portal.service.common.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.common.AttachmentBean;
import com.efast.cafe.portal.bean.common.AttachmentFormBean;
import com.efast.cafe.portal.bean.common.FileUploadReturnBean;
import com.efast.cafe.portal.dao.common.PortalAttachmentDao;
import com.efast.cafe.portal.entity.common.PortalAttachment;
import com.efast.cafe.portal.entity.common.PortalAttachmentBean;
import com.efast.cafe.portal.entity.common.PortalAttachmentForm;
import com.efast.cafe.portal.service.common.IPortalAttachmentService;
import com.efast.cafe.util.AliyunOSSUtil;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.util.UUIDUtils;
@Service
@Transactional
@RestController
public class PortalAttachmentServiceImpl implements IPortalAttachmentService {
	
	@Autowired
	private PortalAttachmentDao portalAttachmentDao;

	@Override
	public List<FileUploadReturnBean> saveAttrachment(List<PortalAttachment> portalAttachments){
		List<FileUploadReturnBean> res = new ArrayList<>();
		String username = SpringWebUtils.getRequestUser();
		String companyCode = SpringWebUtils.getRequestCompany();
		for(PortalAttachment portalAttachment : portalAttachments){
			FileUploadReturnBean bean = new FileUploadReturnBean();
			if(StringUtils.isNotBlank(portalAttachment.getFileId())){
				PortalAttachment att = this.api_queryAttachmentById(Integer.parseInt(String.valueOf(portalAttachment.getId())));
				bean.setAttname(portalAttachment.getRealName());
				portalAttachment.setCreateUser(username);
				portalAttachment.setLastUpdateUser(username);
				portalAttachment.setCompanyCode(companyCode);
				portalAttachment.setCurrentVersion(new BigDecimal(att.getCurrentVersion()).add(new BigDecimal("0.1")).floatValue());
				portalAttachmentDao.saveAttrachment(portalAttachment);
				PortalAttachmentForm portalAttachmentForm = portalAttachmentDao.queryAttachmentFormById(Integer.parseInt(String.valueOf(portalAttachment.getId())), SpringWebUtils.getRequestCompany());
				if(portalAttachmentForm!=null){
					portalAttachmentForm.setAttachmentId(portalAttachment.getId());
					portalAttachmentForm.setCreateTime(new Date());
					portalAttachmentForm.setLastUpdateTime(new Date());
					portalAttachmentForm.setCreateUser(SpringWebUtils.getRequestUser());
					portalAttachmentForm.setLastUpdateUser(SpringWebUtils.getRequestUser());
					List<PortalAttachmentForm> portalAttachmentForms = new ArrayList<>() ;
					portalAttachmentForms.add(portalAttachmentForm);
					portalAttachmentDao.saveAttachmentForm(portalAttachmentForms,SpringWebUtils.getRequestCompany(),SpringWebUtils.getRequestUser());
				}
			}else{
				bean.setAttname(portalAttachment.getRealName());
				portalAttachment.setCreateUser(username);
				portalAttachment.setLastUpdateUser(username);
				portalAttachment.setCompanyCode(companyCode);
				portalAttachment.setCurrentVersion(0.1F);
				portalAttachment.setFileId(UUIDUtils.getUUID32());
				portalAttachmentDao.saveAttrachment(portalAttachment);
			}


			bean.setFileType(portalAttachment.getFileType());
			bean.setAttid(portalAttachment.getId());
            bean.setCreateUser(username);
			bean.setUserRealName(SpringWebUtils.getRequestUserObj().getReal_name());
            bean.setLastUpdateUser(username);
            bean.setCompanyCode(companyCode);
            bean.setCurrentVersion(portalAttachment.getCurrentVersion());
            bean.setFileId(portalAttachment.getFileId());
			bean.setSize(portalAttachment.getFileSize());
			bean.setUrl(portalAttachment.getFilePath());
			bean.setCreateTime(new Date());
			res.add(bean);
		}
		return res;
	}

	@Override
	public void deleteAttrachment(Integer attrachmentId) {
		portalAttachmentDao.deleteAttrachment(attrachmentId,SpringWebUtils.getRequestCompany());
	}

	@Override
	public List<AttachmentBean> findAttachList(String formId, String group) {
		return portalAttachmentDao.findAttachList(formId, group, SpringWebUtils.getRequestCompany());
	}
	
	@Override
	public List<List<AttachmentBean>> findAttachListByTask( AttachmentFormBean attach) {
		List<List<AttachmentBean>> listResult=new ArrayList<>();
		if(attach.getAttachments()!=null&&attach.getAttachments().size()>0){
			for(int i=0;i<attach.getAttachments().size();i++){
				listResult.add(portalAttachmentDao.findAttachList(attach.getAttachments().get(i).getFormId(),attach.getAttachments().get(i).getGroupId(), SpringWebUtils.getRequestCompany()));

			}
		}
		return listResult;
	}

	@Override
	public void saveAttachmentForm(String formId,@RequestBody List<PortalAttachmentForm> list) {
		portalAttachmentDao.deleteAttachmentForm(formId);
		if(null!=list && list.size()>0){
			portalAttachmentDao.saveAttachmentForm(list,SpringWebUtils.getRequestCompany(),SpringWebUtils.getRequestUser());
		}
		
	}

	@Override
	public PortalAttachment queryAttachmentById(Integer id) {
		return portalAttachmentDao.queryAttachmentById(id, SpringWebUtils.getRequestCompany());
	}
	
	
	
	
	
	public String queryTempDownloadUrl(
			Integer attachmentId,String time) {
		String url=ConfigUtil.getValue("oss.sts.img.accountendpoint");
		String accessKeyId =ConfigUtil.getValue("oss.manage.accesskeyid");
		String accessKeySecret =ConfigUtil.getValue("oss.manage.accesskeysecret");	
		String bucket=ConfigUtil.getValue("oss.sts.bucket");	
		PortalAttachment att = queryAttachmentById(attachmentId);
		String filePath=att.getSysName();		
		String realName=att.getRealName();
		return	AliyunOSSUtil.getDownLoadUrl(url, filePath, accessKeyId, accessKeySecret, bucket,realName,time);
	

	}

    @Override
    public void attachmentEdit(AttachmentBean bean) {
        PortalAttachment att = this.api_queryAttachmentById(Integer.parseInt(String.valueOf(bean.getAttachmentId())));

//        if(!att.getRealName().equals(bean.getRealName())){
//			att.setCurrentVersion(new BigDecimal(att.getCurrentVersion()).add(new BigDecimal("0.1")).floatValue());
//
//			portalAttachmentDao.saveAttrachment(att);
//			PortalAttachmentForm portalAttachmentForm = portalAttachmentDao.queryAttachmentFormById(Integer.parseInt(String.valueOf(bean.getAttachmentId())), SpringWebUtils.getRequestCompany());
//			if(portalAttachmentForm!=null){
//				portalAttachmentForm.setAttachmentId(att.getId());
//				portalAttachmentForm.setCreateTime(new Date());
//				portalAttachmentForm.setLastUpdateTime(new Date());
//				portalAttachmentForm.setCreateUser(SpringWebUtils.getRequestUser());
//				portalAttachmentForm.setLastUpdateUser(SpringWebUtils.getRequestUser());
//				List<PortalAttachmentForm> portalAttachmentForms = new ArrayList<>() ;
//				portalAttachmentForms.add(portalAttachmentForm);
//				portalAttachmentDao.saveAttachmentForm(portalAttachmentForms,SpringWebUtils.getRequestCompany(),SpringWebUtils.getRequestUser());
//			}
//		}else{
			att.setRealName(bean.getRealName());
			att.setId(Integer.parseInt(String.valueOf(bean.getAttachmentId())));
			att.setFileComments(att.getFileComments()!=null?(att.getFileComments()+bean.getFileComments()):bean.getFileComments());
			portalAttachmentDao.updateAttrachmentComments(att);
//		}

    }

	@Override
	public List<AttachmentBean> attachmentListTable(String formId, String groupId) {
		return portalAttachmentDao.findAttachListByTable(formId, groupId, SpringWebUtils.getRequestCompany());
	}


	/* (非 Javadoc)
	    * 
	    * 
	    * @param id
	    * @return
	    * @see com.efast.cafe.portal.remote.common.PortalAttachmentService#api_queryAttachmentById(java.lang.Integer)
	    */
	    
	@Override
	public PortalAttachment api_queryAttachmentById(Integer id) {
		return portalAttachmentDao.queryAttachmentById(id, SpringWebUtils.getRequestCompany());
	}

	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @param portalAttachments
	    * @return
	    * @see com.efast.cafe.portal.remote.common.PortalAttachmentService#api_saveAttrachment(java.util.List)
	    */
	    
	@Override
	public List<FileUploadReturnBean> api_saveAttrachment(@RequestBody List<PortalAttachment> portalAttachments) {
		if(portalAttachments!=null){
			return saveAttrachment(portalAttachments);
		}
		return null;
	}
	
	@Override
	public List<PortalAttachmentBean> queryAttachmentFormListByFormId(String formId) {
		return portalAttachmentDao.queryAttachmentFormListByFormId(formId);
	}

	@Override
	public List<FileUploadReturnBean> api_saveAttrachment(String companyCode, String username,
			@RequestBody(required = false)List<PortalAttachment> portalAttachments) {
		List<FileUploadReturnBean> res = new ArrayList<>();
		if(portalAttachments!=null){
			for(PortalAttachment portalAttachment : portalAttachments){
				FileUploadReturnBean bean = new FileUploadReturnBean();
				bean.setAttname(portalAttachment.getRealName());
				portalAttachment.setCreateUser(username);
				portalAttachment.setLastUpdateUser(username);
				portalAttachment.setCompanyCode(companyCode);
				portalAttachmentDao.saveAttrachment(portalAttachment);
				bean.setAttid(portalAttachment.getId());
				res.add(bean);
			}
		}
		return res;
	}

}
